﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CANO.Data.DAOs
{
    public class Event
    {
        public int eventID { get; set; }
        public int typeID { get; set; }
        public string time { get; set; }
        public string eventTypeDisplay { get; set; }
        public string description { get; set; }
        public DateTime dateTimeEvent { get; set; }

        private string _dtEventShort;
        public string dateTimeEventShort
        {
            get { return Convert.ToDateTime(_dtEventShort).ToString("H:mm"); }
            set { _dtEventShort = value; }
        }

        public List<UsuarioDAO> Users { get; set; }
        public List<ContactoDAO> Contacts { get; set; }

        public List<Event> GetAllEvents()
        {
            List<EntrevistaDAO> ListEntrevistas = new EntrevistaDAO().GetCurrentWeekManager();
            List<CompromisoDAO> ListCompromisos = new CompromisoDAO().GetCurrentWeekManager();

            List<Event> ListEvents = new List<Event>();

            foreach (var item in ListEntrevistas)
            {
                Event evento = new Event();
                evento.eventID = item.EntrevistaID;
                evento.typeID = Convert.ToInt32(CANO.Data.Helpers.Enums.TipoEvento.Entrevista);
                evento.time = item.Hora.TimeOfDay.ToString();
                evento.eventTypeDisplay = "Entrevista";
                evento.dateTimeEvent = item.Hora;
                evento.dateTimeEventShort = item.Hora.ToShortTimeString();
                evento.Users = item.CompartidoCon;
                evento.Users.Add(new UsuarioDAO().getUser(item.Usuario));
                evento.Contacts = new List<ContactoDAO>();
                evento.Contacts.Add(new ContactoDAO() { FullName = item.ContactoCompradorDisplay });
                evento.Contacts.Add(new ContactoDAO() { FullName = item.ContactoVendedorDisplay });
                ListEvents.Add(evento);
            }

            foreach (var item in ListCompromisos)
            {
                Event evento = new Event();
                evento.eventID = item.CompromisoID;
                evento.typeID = Convert.ToInt32(CANO.Data.Helpers.Enums.TipoEvento.Compromiso);
                evento.time = item.FechaCompromiso.TimeOfDay.ToString();
                evento.eventTypeDisplay = new TipoCompromisoDAO().GetTipoCompromisoByCompromisoID(item.CompromisoID);
                evento.dateTimeEvent = item.FechaCompromiso;
                evento.dateTimeEventShort = item.FechaCompromiso.ToShortTimeString();
                evento.Users = item.CompartidoCon;
                evento.Contacts = new List<ContactoDAO>();
                evento.Contacts.Add(new ContactoDAO() { FullName = item.ContactoAsociadoDisplay });
                evento.Contacts.Add(new ContactoDAO() { FullName = item.ContactoAsociado2Display });
                ListEvents.Add(evento);
            }
            //ListEvents.OrderBy(e => e.dateTimeEvent);
            ListEvents.Sort((x, y) => DateTime.Compare(x.dateTimeEvent, y.dateTimeEvent));
            return ListEvents;
        }

        internal List<Event> GetEventsByUserID(int CurrentUserID)
        {
            List<EntrevistaDAO> ListEntrevistas = new EntrevistaDAO().GetCurrentWeekByUser(CurrentUserID);
            List<CompromisoDAO> ListCompromisos = new CompromisoDAO().GetCurrentWeekByUser(CurrentUserID);

            List<Event> ListEvents = new List<Event>();

            foreach (var item in ListEntrevistas)
            {
                Event evento = new Event();
                evento.eventID = item.EntrevistaID;
                evento.typeID = Convert.ToInt32(CANO.Data.Helpers.Enums.TipoEvento.Entrevista);
                evento.time = item.Hora.TimeOfDay.ToString();
                evento.eventTypeDisplay = "Entrevista";
                evento.dateTimeEvent = item.Hora;
                evento.dateTimeEventShort = item.Hora.ToShortTimeString();
                ListEvents.Add(evento);
            }

            foreach (var item in ListCompromisos)
            {
                Event evento = new Event();
                evento.eventID = item.CompromisoID;
                evento.typeID = Convert.ToInt32(CANO.Data.Helpers.Enums.TipoEvento.Compromiso);
                evento.time = item.FechaCompromiso.TimeOfDay.ToString();
                evento.eventTypeDisplay = "Compromiso";
                evento.dateTimeEvent = item.FechaCompromiso;
                evento.dateTimeEventShort = item.FechaCompromiso.ToShortTimeString();
                ListEvents.Add(evento);
            }
            ListEvents.Sort((x, y) => DateTime.Compare(x.dateTimeEvent, y.dateTimeEvent));
            return ListEvents;
        }
    }

}